package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.swing.text.Utilities;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService  roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        // 判断是否存在id
        boolean isUpdate = roomSubmitVo.getId() != null;
        //不是空就进行修改 如果是空就增加
        // 0. 删除或者更新房间基本信息
        super.saveOrUpdate(roomSubmitVo);
        if (isUpdate) {
            // 先删除再插入
            // 1. 删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            // 2. 删除属性信息
            LambdaQueryWrapper<RoomAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            attrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(attrValueLambdaQueryWrapper);
            // 3.删除配套信息
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            // 4.删除标签信息
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            // 5.删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            // 6.删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }

        // 插入前需要判断集合是否存在
        List<GraphInfo> graphInfoList = new ArrayList<>();
       if(!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())){
           roomSubmitVo.getGraphVoList().forEach(graphVo -> {
               GraphInfo graphInfo = new GraphInfo();
               // url
               graphInfo.setUrl(graphVo.getUrl());
               // 名字
               graphInfo.setName(graphVo.getName());
               // 设置类型
               graphInfo.setItemType(ItemType.ROOM);
               // 设置roomID
               graphInfo.setItemId(roomSubmitVo.getId());
               graphInfoList.add(graphInfo);
           });
           graphInfoService.saveBatch(graphInfoList);
       }

       //插入属性
        List<RoomAttrValue>  roomAttrValueList = new ArrayList<>();
       if (!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())){
           roomSubmitVo.getAttrValueIds().forEach(attrValueId -> {
               RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                       .attrValueId(attrValueId)
                       .roomId(roomSubmitVo.getId())
                       .build();
               roomAttrValueList.add(roomAttrValue);
           });
       }
       roomAttrValueService.saveBatch(roomAttrValueList);

       // 插入配套信息
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())){
            roomSubmitVo.getFacilityInfoIds().forEach(facilityId -> {
                RoomFacility facility = RoomFacility.builder()
                        .facilityId(facilityId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomFacilityList.add(facility);
            });
            roomFacilityService.saveBatch(roomFacilityList);
        }

        // 标签信息
        List<RoomLabel> roomLabelList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())){
            roomSubmitVo.getLabelInfoIds().forEach(labelId -> {
                RoomLabel roomLabel = RoomLabel.builder()
                        .roomId(roomSubmitVo.getId())
                        .labelId(labelId)
                        .build();
                roomLabelList.add(roomLabel);
            });
            roomLabelService.saveBatch(roomLabelList);
        }

        // 支付方式
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())){
            roomSubmitVo.getPaymentTypeIds().forEach(paymentTypeId -> {
                RoomPaymentType paymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomPaymentTypeList.add(paymentType);
            });
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        // 租期列表
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())){
            roomSubmitVo.getLeaseTermIds().forEach(leaseTermId -> {
               RoomLeaseTerm roomLeaseTerm  = RoomLeaseTerm.builder()
                       .leaseTermId(leaseTermId)
                       .roomId(roomSubmitVo.getId())
                       .build();
               roomLeaseTermList.add(roomLeaseTerm);
            });
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    /**
     * 分页查询房间信息
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return  roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //根据id查询基本信息
        RoomInfo roomInfo = super.getById(id);
        if(roomInfo == null) {
            return null;
        }
        //公寓信息
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        //图片
        List<GraphVo> graphInfoList  = graphInfoMapper.selectGraphListByRoomId(ItemType.ROOM,id);
        //属性
        List<AttrValueVo>  attrValueVoList = attrValueMapper.selectAttrValueByRoomId(id);
        //配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByRoomId(id);
        //标签
        List<LabelInfo>  labelInfoList = labelInfoMapper.selectLableInfoByRoomId(id);
        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);
        // 租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermByRoomId(id);


        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    /**
     * 根据ID删除房间信息
     * @param id
     */
    @Override
    public void removeRoomById(Long id) {
        // 1.删除RoomInfo
        super.removeById(id);

        // 删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoQueryWrapper);

        // 删除属性值
        LambdaQueryWrapper<RoomAttrValue> attrValueVoQueryWrapper = new LambdaQueryWrapper<>();
        attrValueVoQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(attrValueVoQueryWrapper);

        //删除设备
        LambdaQueryWrapper<RoomFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityLambdaQueryWrapper);

        //删除标签
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);


        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除租赁信息
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(termQueryWrapper);

    }
}



















